//
//  RealTimeViewController.m
//  mendianjieke
//
//  Created by 响当当 on 7/30/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "RealTimeViewController.h"
#import "RealTimeView.h"

@interface RealTimeViewController ()
@property (nonatomic, strong)RealTimeView *realTimeView;
@property (nonatomic, strong)NSString *str;
@property (nonatomic, strong)UIView *noCountView;
@end

@implementation RealTimeViewController

- (UIView *)noCountView
{
    if (!_noCountView)
       {
           _noCountView = [Utilties getNoCountViewWithFrame:CGRectMake(SCREEN_WIDTH/2-132*kUIScaleBasePlus,SCREEN_HEIGHT/2-80*kUIScaleBasePlus,264*kUIScaleBasePlus,264*kUIScaleBasePlus)
                                                      image:@"no_customer"
                                                      title:@"暂时没有服务中的客户哦~\n点击左下方实时按钮，开始接待吧"];
       }
       return _noCountView;
}

- (RealTimeView *)realTimeView
{
    if (!_realTimeView)
    {
        _realTimeView = [[RealTimeView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-TABBAR_HEIGHT)];
        WeakSelf(self)
        _realTimeView.getCustomerSuccessBlock = ^{
            [weakSelf getInfo:@"0"];
            weakSelf.str = @"1";
        };
        
        _realTimeView.reloadDataBlock = ^{
            [weakSelf getInfo:@"0"];
        };
        
        _realTimeView.foodReloadDataBlock = ^(NSString *page) {
            [weakSelf getInfo:@"0" page:page];
        };
        
        [_realTimeView getInfo:nil infoType:@"0"];
    }
    return _realTimeView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = LCHexColor(0xF2F2F2);
    
    [self setTitle:@"实时"];

    [self.view addSubview:self.realTimeView];
    
    self.str = @"0";
    
    [self getInfo:@"0"];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DoubleClickRefresh:) name:@"DOUBLEREFRESH" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresList:) name:@"RefreshLabel" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"RefreshToken" object:nil];

}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
        
    NSString *isFirstGetMessage = LCUserDefaultsGet(@"notificationCount");

    if (isFirstGetMessage.integerValue == 0)
    {
        [self.headBtn removeFromSuperview];
        LCUserDefaultsSet(@"1", @"notificationCount");
        LCUserDefaultsSyn;
    }
}

- (void)DoubleClickRefresh:(NSNotification *)notfi
{
     if (current_User.hasLogin.integerValue == 1 &&[self.topTitleLabel.text isEqualToString:@"实时"])
       {
           
           [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DOUBLEREFRESH" object:nil];
           
           NSDictionary *pramaters = @{
                                       @"type" : @"0",
                                       @"id" : @"",
                                       @"dataTime":[Utilties getNowTimeTimestamp],
                                       @"page":@"1",
                                       @"size":@"10"
                                       };
           

           WeakSelf(self)
           [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
               
               DISMISS_SVP(1.0)
               
               NSArray *arr = [NSArray arrayWithArray:acceptData];
                                     
               if (arr.count == 0)
               {
                   [weakSelf.view addSubview:weakSelf.noCountView];
               }
               else
               {
                   if (weakSelf.noCountView)
                   {
                       [weakSelf.noCountView removeFromSuperview];
                   }
                                  
               }
               
               [weakSelf.realTimeView getRefreshList:IFNULL(acceptData,[NSArray array]) infoType:@"1"];

               [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"DOUBLEREFRESH" object:nil];
               
           } fail:^(NSError * _Nullable error) {
               
           }];
       }
}

- (void)showRealTime:(NSNotification *)notfi
{
    
    NSString *isFirstGetMessage = LCUserDefaultsGet(@"notificationCount");
    if (isFirstGetMessage.integerValue == 1)
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate gotoHome];
//        LCUserDefaultsSet(@"0", @"notificationCount");
//        LCUserDefaultsSyn;
    }
    
    
    NSDictionary *dic = notfi.userInfo;
             
    if ([[Utilties getCurrentVC] isKindOfClass:[RealTimeViewController class]] && current_User.hasLogin.integerValue == 1)
    {
        NSDictionary *pramaters = @{
                                        @"type" : @"0",
                                        @"id" : dic[@"id"],
                                        @"dataTime":@"",
                                        @"page":@"1",
                                        @"size":@"10"
                                    };

        WeakSelf(self)
        [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
                     
                DISMISS_SVP(1.0)

                NSArray *arr = [NSArray arrayWithArray:acceptData];
                     
                if (arr.count == 0)
                {
                    [weakSelf.view addSubview:weakSelf.noCountView];
                }
                else
                {
                    if (weakSelf.noCountView)
                    {
                        [weakSelf.noCountView removeFromSuperview];
                    }
                }
                [weakSelf.realTimeView getInfoRealTime:IFNULL(acceptData,[NSArray array]) infoType:@"1"];
                [weakSelf getInfoNum:@"1"];


                 } fail:^(NSError * _Nullable error) {
                     
                 }];
             }
}

- (void)reFresList:(NSNotification *)notfi
{
    
    if (current_User.hasLogin.integerValue == 1)
    {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RefreshLabel" object:nil];

        NSDictionary *pramaters = @{
                                    @"type" : @"0",
                                    @"id" : @"",
                                    @"dataTime":[Utilties getNowTimeTimestamp],
                                    @"page":@"1",
                                    @"size":@"10"
                                    };
        

        WeakSelf(self)
        [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
            DISMISS_SVP(1.0)
            
            NSArray *arr = [NSArray arrayWithArray:acceptData];
                       
            if (arr.count == 0)
            {
                [weakSelf.view addSubview:weakSelf.noCountView];
            }
            else
            {
                if (weakSelf.noCountView)
                {
                    [weakSelf.noCountView removeFromSuperview];
                }
            }
            [weakSelf.realTimeView getRefreshList:IFNULL(acceptData,[NSArray array]) infoType:@"1"];
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresList:) name:@"RefreshLabel" object:nil];
            [weakSelf getInfoNum:@"1"];


        } fail:^(NSError * _Nullable error) {
            
        }];
    }
}

- (void)reFresToken:(NSNotification *)notfi
{
    
    if (current_User.hasLogin.integerValue == 1)
    {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RefreshToken" object:nil];
        
        NSDictionary *pramaters = @{
                                    @"type" : @"0",
                                    @"id" : @"",
                                    @"dataTime":[Utilties getNowTimeTimestamp],
                                    @"page":@"1",
                                    @"size":@"10"
                                    };
        

        WeakSelf(self)
        [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
            
            DISMISS_SVP(1.0)
            
            NSArray *arr = [NSArray arrayWithArray:acceptData];
                                  
            if (arr.count == 0)
            {
                [weakSelf.view addSubview:weakSelf.noCountView];
            }
            else
            {
                if (weakSelf.noCountView)
                {
                    [weakSelf.noCountView removeFromSuperview];
                }
                               
            }
            
            [weakSelf.realTimeView getRefreshList:IFNULL(acceptData,[NSArray array]) infoType:@"1"];

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"RefreshToken" object:nil];
            [weakSelf getInfoNum:@"1"];

            
        } fail:^(NSError * _Nullable error) {
            
        }];
    }
}

- (void)getInfo:(NSString *)type
{
    NSDictionary *pramaters = @{
                                @"type" : type,
                                @"id" : @"",
                                @"dataTime":[Utilties getNowTimeTimestamp],
                                @"page":@"1",
                                @"size":@"10"
                                 };
    [SVProgressHUD showWithStatus:@"加载中"];
    WeakSelf(self)
    [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
        
        if ([weakSelf.str isEqualToString:@"1"])
        {
            DISMISS_SVP(1.0)
            weakSelf.str = @"0";
        }
        
        DISMISS_SVP(1.0);

        NSArray *arr = [NSArray arrayWithArray:acceptData];
        
        if (arr.count == 0)
        {
            [weakSelf.view addSubview:weakSelf.noCountView];
        }
        else
        {
            if (weakSelf.noCountView)
            {
                [weakSelf.noCountView removeFromSuperview];
            }
        }
        
        [weakSelf.realTimeView getInfo:IFNULL(acceptData,[NSArray array]) infoType:@"0"];

        
        [weakSelf getInfoNum:@"1"];

    } fail:^(NSError * _Nullable error) {
        
    }];
}

//上拉刷新
- (void)getInfo:(NSString *)type page:(NSString *)page
{
    NSDictionary *pramaters = @{
                                   @"type" : type,
                                   @"id" : @"",
                                   @"dataTime":[Utilties getNowTimeTimestamp],
                                   @"page":page,
                                   @"size":@"10"
                                    };
//       [SVProgressHUD showWithStatus:@"加载中"];
       WeakSelf(self)
       [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
           
           if ([weakSelf.str isEqualToString:@"1"])
           {
               DISMISS_SVP(1.0)
               weakSelf.str = @"0";
           }

           [weakSelf.realTimeView getFootRefreshList:IFNULL(acceptData,[NSArray array]) infoType:@"1"];
           
       } fail:^(NSError * _Nullable error) {
           
       }];
}


- (void)getInfoNum:(NSString *)type
{
    NSDictionary *pramaters = @{
                                @"type" : type,
                                @"id" : @"",
                                @"dataTime":@""
                                };
    
    [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
        NSArray *arr = IFNULL(acceptData,[NSArray array]);
        
        if (arr.count!=0)
        {
            NSString *bageNum = [NSString stringWithFormat:@"%lu",(unsigned long)arr.count];
            LCUserDefaultsSet(bageNum, @"bageNum");
            LCUserDefaultsSyn;
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate showTabCount:bageNum];
        }
        else
        {
            NSString *bageNum = @"0";
            LCUserDefaultsSet(bageNum, @"bageNum");
            LCUserDefaultsSyn;
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate showTabCount:bageNum];
        }
        
       
    } fail:^(NSError * _Nullable error) {
        
    }];
}

@end
